﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Jalal.Library.DataStructure;
using System.Data;
using System.Data.SqlTypes;
using Jalal.Library.Data;
using Jalal.Library.Utility;
using BCGC.DAL;

namespace BCGC.BLL.BusinessObject
{
    public class CMS
    {
        private int cmsId;
        public int CMSId
        {
            get { return cmsId; }
            protected set { cmsId = value; }
        }

        private string cmsTitle;
        public string CMSTitle
        {
            get { return cmsTitle; }
            protected set { cmsTitle = value; }
        }

        private string cmsContent;
        public string CMSContent
        {
            get { return cmsContent; }
            set { cmsContent = value; }
        }

        private DateTime lastUpdate;
        public DateTime LastUpdate
        {
            get { return lastUpdate; }
            set { lastUpdate = value; }
        }

        public enum CMSPages
        {
            About_Us_Main_Content = 1,
            Contact_Us_Main_Content = 2,
            FAQ_Main_Content = 3
        }

        public CMS()
        {
            this.cmsId = 0;
            this.cmsTitle = string.Empty;
            this.cmsContent = string.Empty;
            this.lastUpdate = SqlDateTime.MinValue.Value;
        }

        public CMS(DataRow dataRow)
        {
            if (dataRow != null)
            {
                if (dataRow.Table.Columns.Contains("CmsId"))
                    this.cmsId = (int)CommonUtility.FilterNull(dataRow["CmsId"], typeof(int));
                if (dataRow.Table.Columns.Contains("CmsTitle"))
                    this.cmsTitle = (string)CommonUtility.FilterNull(dataRow["CmsTitle"], typeof(string));
                if (dataRow.Table.Columns.Contains("LastUpdate"))
                    this.lastUpdate = (DateTime)CommonUtility.FilterNull(dataRow["LastUpdate"], typeof(DateTime));
                if (dataRow.Table.Columns.Contains("CmsContent"))
                    this.cmsContent = (string)CommonUtility.FilterNull(dataRow["CmsContent"], typeof(string));
            }
            else
            {
                this.cmsId = 0;
                this.cmsTitle = string.Empty;
                this.cmsContent = string.Empty;
                this.lastUpdate = SqlDateTime.MinValue.Value;
            }
        }

        public static List<CMSPages> GetCMSPageList()
        {
            List<CMSPages> cmsPages = new List<CMSPages>();

            cmsPages.Add(CMSPages.About_Us_Main_Content);
            cmsPages.Add(CMSPages.Contact_Us_Main_Content);
            cmsPages.Add(CMSPages.FAQ_Main_Content);

            return cmsPages;
        }

        private static ReadWriteCustomCollection<CMS> LoadFromDataTable(DataTable data)
        {
            ReadWriteCustomCollection<CMS> result = new ReadWriteCustomCollection<CMS>();

            if (data != null && data.Rows.Count > 0)
            {
                foreach (DataRow row in data.Rows)
                {
                    result.Add(new CMS(row));
                }
            }
            return result;
        }

        public static CMS GetCMSById(int cmsId)
        {
            DataFactory.GetDataMiner().BeginTransaction(true);
            CMS cms = null;
            DataTable data = CMSData.GetCMSById(cmsId);
            if (data != null && data.Rows.Count > 0)
                cms = new CMS(data.Rows[0]);

            DataFactory.GetDataMiner().CommitTransaction();

            return cms;
        }

        public bool Update()
        {
            DataFactory.GetDataMiner().BeginTransaction(true);

            int count = CMSData.UpdateCMSData(this.cmsId, this.cmsTitle, this.cmsContent, this.lastUpdate);

            DataFactory.GetDataMiner().CommitTransaction();

            return (count == 1);
        }
    }
}
